package com.sentiment.mail;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import org.apache.log4j.Logger; 

/** 
  *  类功能 : 处理意见反馈邮件发送 调用 jar:activation.jar 、 mail.jar 
  */ 

public class MailBean { 

    public static Logger log = null ; 

    static { 
       log = Logger.getLogger (MailBean. class ); 
    } 
    
    // smtpHost 发件人所用到的 smtp 服务器 
    String smtpHost = "smtp.sina.com" ; 
    // from 发件人邮箱 
    String from = "grateful_r@sina.com" ; 
    // to 收件人邮箱 
    String to = "mbrasser@d-ear.com" ; 
    // subject 邮件标题 
    String subject = "receive a mail from d-ear@sina.com" ; 
   
    // theMessage 邮件内容 
    StringBuffer theMessage = new StringBuffer(); 

    /** 
      * 固定的给931094193@qq.com ,1547073674@qq.com 发送邮件 
      * 
      * @param smtpHost 
      * @param from 
      * @param subject 
      * @param messageText 
      * @throws MessagingException 
     * @throws UnsupportedEncodingException 
      */ 

    public void sendMessage(String smtpHost, String from, 
           String subject, String messageText) throws MessagingException, UnsupportedEncodingException { 
       SmtpAuth sa = new SmtpAuth(); 
 //      sa.getuserinfo( "grateful_r@sina.com" , "931094193@qq.com" ); 
       java.util.Properties props = new java.util.Properties(); 
       props.put( "mail.smtp.auth" , "true" ); 
       props.put( "mail.smtp.host" , smtpHost); 
       System. out .println( "Constructing message- from=" + from + " to=" + to ); 
       InternetAddress fromAddress = new InternetAddress(from); 
       InternetAddress[] toAddresss = new InternetAddress[2]; 
       toAddresss[0] = new InternetAddress( "931094193@qq.com" ); 
       toAddresss[1] = new InternetAddress( "1547073674@qq.com" ); 
       int i = 0; 
       while (i < toAddresss. length ) { 
           Session mailSession = Session.getDefaultInstance (props, sa); 
           MimeMessage testMessage = new MimeMessage(mailSession); 
           testMessage.setFrom(fromAddress); 
           testMessage.addRecipient(javax.mail.Message.RecipientType. TO , 
                  toAddresss[i]); 
           testMessage.setSentDate( new java.util.Date()); 
           testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));
           testMessage.setContent(messageText, "text/html;charset=gb2312");
           Transport transport = mailSession.getTransport("smtp");
           transport.connect(smtpHost, "grateful_r@sina.com", "931094193@qq.com");
           transport.sendMessage(testMessage, testMessage.getAllRecipients());
      //     transport.sendMessage(testMessage, testMessage.getAllRecipients());
           System. out .println( "A mail have been sent!" ); 
           i++; 
       } 
    } 

    /* 
      * 由 sina 服务器向目的邮箱发送邮件 
      * 邮件发送处理 @param stmHost,from,to,subject,messageText 
      */ 

    public void sendMessage(String smtpHost, String from, String to, 
           String subject, String messageText) throws MessagingException, UnsupportedEncodingException { 
       SmtpAuth sa = new SmtpAuth(); 
  //     sa.getuserinfo( "grateful_r" , "931094193@qq.com" ); 
       java.util.Properties props = new java.util.Properties(); 
       props.put( "mail.smtp.auth" , "true" ); 
       props.put( "mail.smtp.host" , smtpHost); 
       System. out .println( "Constructing message- from=" + from + " to=" + to); 
       InternetAddress fromAddress = new InternetAddress(from); 
       InternetAddress toAddresss = new InternetAddress(to); 
           Session mailSession = Session.getDefaultInstance (props, sa); 
           MimeMessage testMessage = new MimeMessage(mailSession); 
           testMessage.setFrom(fromAddress); 
           testMessage.addRecipient(javax.mail.Message.RecipientType. TO , 
                  toAddresss); 
           testMessage.setSentDate( new java.util.Date()); 
         //  testMessage.setSubject(subject); 
           testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));
           testMessage.setContent(messageText, "text/html;charset=gb2312");
     //      testMessage.setText(messageText); 
           Transport transport = mailSession.getTransport("smtp");
           transport.connect(smtpHost, "grateful_r@sina.com", "931094193@qq.com");
           transport.sendMessage(testMessage, testMessage.getAllRecipients());
      //     transport.sendMessage(testMessage, testMessage.getAllRecipients());
    //       Transport.send (testMessage); 
           System. out .println( "A mail have been sent to " + to); 
           
    } 

    /** 
      * 功能：群发功能 , 把所有的目的邮箱作为一个数组参数传入 
      * 
      * @param smtpHost 
      * @param from 
      * @param to 目的邮箱数组 
      * @param subject 
      * @param messageText 
      * @throws MessagingException 
     * @throws UnsupportedEncodingException 
      */ 

    public void sendMessage(String smtpHost, String from, List<String> to, 
           String subject, String messageText) throws MessagingException, UnsupportedEncodingException { 
       SmtpAuth sa = new SmtpAuth(); 
//       sa.getuserinfo( "d-ear" , "123abc" ); 
       java.util.Properties props = new java.util.Properties(); 
       props.put( "mail.smtp.auth" , "true" ); 
       props.put( "mail.smtp.host" , smtpHost); 
       System. out .println( "Constructing message- from=" + from + " to=" + to); 
       InternetAddress fromAddress = new InternetAddress(from); 
       InternetAddress[] toAddresss = new InternetAddress[to. size() ]; 
       int j = 0;
       for ( String string : to){ 
           toAddresss[j] = new InternetAddress(string);     
           j++;
       } 
       int i = 0; 
       while (i < toAddresss. length ) { 
           Session mailSession = Session.getDefaultInstance (props, sa); 
           MimeMessage testMessage = new MimeMessage(mailSession); 
           testMessage.setFrom(fromAddress); 
           testMessage.addRecipient(javax.mail.Message.RecipientType. TO , 
                  toAddresss[i]); 
           testMessage.setSentDate( new java.util.Date()); 
           testMessage.setSubject(MimeUtility.encodeText(subject,"gb2312","B"));
           testMessage.setContent(messageText, "text/html;charset=gb2312");
           Transport transport = mailSession.getTransport("smtp");
           transport.connect(smtpHost, "grateful_r@sina.com", "931094193@qq.com");
           transport.sendMessage(testMessage, testMessage.getAllRecipients());
      //     transport.sendMessage(testMessage, testMessage.getAllRecipients());
           System. out .println( "A mail have been sent to " +to.get(i)); 
           i++; 

       } 
    } 

    /* 
      * 邮件用户名和密码认证 
      */ 

    static class SmtpAuth extends javax.mail.Authenticator { 

       private String user , password ; 

       public void getuserinfo(String getuser, String getpassword) { 
           user = getuser; 
           password = getpassword; 
       } 

       protected javax.mail.PasswordAuthentication getPasswordAuthentication() { 
           return new javax.mail.PasswordAuthentication( user , password ); 
       } 
    } 

} 

